package com.ruipengkj.component.jpanel;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.TexturePaint;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.ruipengkj.commons.util.Util;
import com.ruipengkj.factory.ImageFactory;

/**
 * 查询面板
 * */
public class QueryPanel extends JPanel {

	private static final long serialVersionUID = 1L;
	private ImageIcon backgroundImageIcon = ImageFactory.loadImage(ImageFactory.QUERY_PANEL_BG);
	private TexturePaint paint = Util.createTexturePaint(backgroundImageIcon.getImage());
	private Image backgroundLeftImage = ImageFactory.loadImage(ImageFactory.QUERY_PANEL_LEFT_BG).getImage();
	private Image backgroundRightImage = ImageFactory.loadImage(ImageFactory.QUERY_PANEL_RIGHT_BG).getImage();
	private Image backgroundLineImage = ImageFactory.loadImage(ImageFactory.QUERY_TITLE_LINE_BG).getImage();
	
	private ImageIcon lib_backgroundImageIcon = ImageFactory.loadImage(ImageFactory.QUERY_TITLE_BG);
	private TexturePaint lib_paint = Util.createTexturePaint(lib_backgroundImageIcon.getImage());
	private Image lib_backgroundLeftImage = ImageFactory.loadImage(ImageFactory.QUERY_TITLE_LEFT_BG).getImage();
	private Image lib_backgroundRightImage = ImageFactory.loadImage(ImageFactory.QUERY_TITLE_RIGHT_BG).getImage();
	private Image lib_icon_Image = ImageFactory.loadImage(ImageFactory.ICON_2).getImage();
	 
	private String title="会员管理";
	private ImageIcon icon=ImageFactory.loadImage(ImageFactory.test_icon2);
	private JPanel main_jp=new JPanel(new BorderLayout());
//	private JPanel main_jp=UIFactory.getInstance().createBoxJPanel(BoxLayout.Y_AXIS);
	public QueryPanel(String title,ImageIcon icon){
		if(title!=null){
			this.title=title;
		}
		if(icon!=null){
			this.icon=icon;
		}
		this.setLayout(new BorderLayout());
		this.setPreferredSize(new Dimension(1024, 80));
		this.init();
	}

	private void init(){
		//左边
		JLabel lin=new JLabel("  ",icon,JLabel.LEFT);
		lin.setHorizontalTextPosition(JLabel.LEFT);
		this.add(lin,BorderLayout.WEST);
		
		//中间
		main_jp.setOpaque(false);
		//标题
//		JPanel llb=new JPanel() {
//			@Override
//			protected void paintComponent(Graphics g) {
//				int leftX=50;
//				Graphics2D g2d = (Graphics2D) g;
//				FontMetrics fm=g2d.getFontMetrics(Util.TITLE_FONT);
//				
//				g2d.drawImage(lib_backgroundLeftImage, leftX, 0, null);
//				g2d.setPaint(lib_paint);
//				int x = lib_backgroundLeftImage.getWidth(null)+leftX;
//				int y = 0;
//				int width = fm.stringWidth(title)+23;
//				int height = getHeight();
////				int width = getWidth() - x- lib_backgroundRightImage.getWidth(null);
////				g2d.fillRect(x, y, width, height);
//				g2d.fillRect(x, y,width, height);
//				g2d.drawImage(lib_backgroundRightImage, x + width, 0, null);
//				
//				g2d.setColor(Color.WHITE);
//				g2d.setFont(Util.TITLE_FONT);
//				g2d.drawImage(lib_icon_Image, x+5, 10, null);
//				g2d.drawString(title, x+20, 20);
//				
//			}
//		};
//		llb.setOpaque(false);
//		llb.setPreferredSize(new Dimension(0, 30));
//		main_jp.add(llb,BorderLayout.NORTH);
		main_jp.add(new JLabel("   "),BorderLayout.WEST);
		this.add(main_jp,BorderLayout.CENTER);
	}

	@Override
	protected void paintComponent(Graphics g) {
		Graphics2D g2d = (Graphics2D) g;
		// fill background.
		g2d.setPaint(paint);
		g2d.fillRect(0, 0, this.getWidth(), this.getHeight());
		// draw left image.
		g2d.drawImage(backgroundLeftImage, 0, 0, null);
		// draw right image.
		int x = this.getWidth() - backgroundRightImage.getWidth(null);
		int y = 0;
		g2d.drawImage(backgroundRightImage, x, y, null);
		
		g2d.drawImage(backgroundLineImage, icon.getIconWidth()+30, y, null);
	}

	public void addPanel(JPanel comp){
		comp.setOpaque(false);
		main_jp.add(comp,BorderLayout.CENTER);
		main_jp.updateUI();
	}
	
//	public void addBorderPanel(JPanel comp){
//		main_jp=new JPanel(new BorderLayout());
//		comp.setOpaque(false);
//		main_jp.add(comp,BorderLayout.CENTER);
//		main_jp.updateUI();
//	}
//	public void addBoxPanel(JPanel comp){
//		main_jp=UIFactory.getInstance().createBoxJPanel(BoxLayout.Y_AXIS);
//		comp.setOpaque(false);
//		main_jp.add(comp,BorderLayout.CENTER);
//		main_jp.updateUI();
//	}
}
